package com.burgeon.backend.EnterpriseWeChat.rest;
/**
 * 获取符合条件的导购 用于生成企业微信员工 参数ids
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.EmpData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class QueryErpEmpDataByIDs extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QueryErpEmpDataByIDs(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<EmpData> getData(String[] arr) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(arr), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception();
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<EmpData> data = new ArrayList<EmpData>();
        for (int i=0;i<jsonArray.length();i++){
            EmpData empData = new EmpData();
            empData.setId(jsonArray.getJSONArray(i).getString(0));
            empData.setName(jsonArray.getJSONArray(i).getString(1));
            empData.setCode(jsonArray.getJSONArray(i).getString(2));
            empData.setPhone(jsonArray.getJSONArray(i).getString(3));
            data.add(empData);
        }

        return data;
    }

    private MultiValueMap Data(String[] arr) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("NAME");//员工名
        columns.add("NO");//编号
        columns.add("PHONE");


        params.put("columns", columns);
        params.put("table", "C_V_EMPLOYEE");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();

        expr1.put("column", "PHONE");
        expr1.put("condition", "is not null");
        HashMap expr2 = new HashMap();
        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();
        expr2v1.put("column","ISACTIVE");
        expr2v1.put("condition","Y");
        expr2v2.put("column","ID");
        expr2v2.put("condition", "\" IN ("+ toString(arr)+")\"");

        expr2.put("combine","and");
        expr2.put("expr1",expr2v1);
        expr2.put("expr2",expr2v2);

        expr2.put("column", "'ISACTIVE'");
        expr2.put("condition", "Y");


        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }
    private String toString(String[] data){

        return Arrays.toString(data).replace("\"","'")
                .replace("[","")
                .replace("]","")
                .replace(" ", "");

    }
    public static void main(String[] args) {
        QueryErpEmpDataByIDs g = new QueryErpEmpDataByIDs("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            String[] arr = new String[]{"7097"};
            System.err.println(g.getData(arr));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
